package com.test;


import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * <dl>
 * <dt>Send</dt>
 * <dd>Description:生产者</dd>
 * <dd>Copyright: Copyright (C) 2006</dd>
 * <dd>CreateDate: 2019-3-7</dd>
 * </dl>
 *
 * @author Administrator
 */
public class Send {
    public static void main(String[] args) {
        //与kafka连接，参考官网：http://kafka.apache.org/documentation/#producerapi
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.56.101:9092,192.168.56.101:9093");
        props.put("acks", "all");
//        props.put("delivery.timeout.ms", 30000);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        //key序列化类是哪一个
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        //使用自定义分区类
//        props.put("partitioner.class", "com.test.custom.partition.CustomePartition");
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        for(int i = 0; i < 100; i++){
            producer.send(new ProducerRecord<String, String>("test", Integer.toString(i), Integer.toString(i)));
        }
        //关闭
        producer.close();
    }
}
